<template>
  <div class="page-main">
    <query-page
      ref="queryPageComp"
      v-model:selectedKeys="selectedKeys"
      title="加工内容基础数据维护"
      :form-component-config="formComponentConfig"
      :form-render-config="formRenderConfig"
      :form-render-keys="formRenderKeys"
      :table-render-config="mainform2"
      :table-render-keys="tableRenderKeys"
      :loading="isLoading"
      :data="renderData"
      :total="total"
      :has-line="true"
      row-selection-type="checkbox"
      @query-data="searchHandle"
    >
      <template #container-side>
        <a-button
          v-permission="['base:machining-content:save']"
          type="primary"
          @click="addList"
          >新增</a-button
        >
        <a-button
          v-permission="['base:machining-content:delete']"
          type="dashed"
          :disabled="!selectedKeys.length"
          @click="removeBatch"
          >删除</a-button
        >
        <a-button
          v-permission="['base:machining-content:cancels']"
          type="dashed"
          :disabled="!selectedKeys.length"
          @click="voidBatch"
          >作废</a-button
        >
        <a-button disabled>提交审批</a-button>
      </template>

      <template #operate="{ record }">
        <a-button
          :hidden="
            !removeAndEditorStatus.includes(record.documentPreparationStatus)
          "
          v-permission="['base:machining-content:update']"
          type="text"
          @click="editList(record)"
          >编辑</a-button
        >
        <a-button
          :hidden="
            !removeAndEditorStatus.includes(record.documentPreparationStatus)
          "
          v-permission="['base:machining-content:delete']"
          type="text"
          @click="removeSingle(record)"
          >删除</a-button
        >
        <a-button
          :hidden="!voidStatus.includes(record.documentPreparationStatus)"
          v-permission="['base:machining-content:cancels']"
          type="text"
          @click="voidSingle(record)"
          >作废</a-button
        >
      </template>
    </query-page>

    <myModal
      v-if="showModal"
      :id="editorId"
      :type="modelType"
      @close="closeModal"
      @refresh="searchHandle"
    ></myModal>
  </div>
</template>

<script setup lang="ts">
  import { ref, defineAsyncComponent } from 'vue';
  import {
    formRenderConfig,
    formRenderKeys,
  } from '@/views/basic-configuration/components/Basicdatacontentform';
  import { mainform2 } from '@/views/basic-configuration/singlelist/Singlelistcolums';
  import { Message } from '@arco-design/web-vue';
  import {
    getBasicDataContent,
    deleteList,
    listVoid,
  } from '@/api/Basimanagementapi/Basicdatacontent';
  import useModal from '@/common/hooks/modal';
  import { formComponentConfig } from '@/common/views/baseComponent/formComp/baseFormConfig';
  import queryPage from '@/common/components/query-page/index.vue';
  import { warning } from '@/common/utils/modal-promise';
  import modalPromise from '@/common/utils/modal-promise';

  const myModal = defineAsyncComponent(
    () =>
      import(
        '@/views/basic-configuration/basicdatacontent/basicdatacontentModal.vue'
      )
  );

  const tableRenderKeys = [
    'index',
    'secretLevel',
    'documentPreparationStatusName',
    'machiningCode',
    'machiningName',
    'categoryLv1',
    'categoryLv2',
    'categoryLv3',
    'machiningDescribe',
    'remarks',
    'creator',
    'createTime',
    'operate',
  ];

  const { showModal, openModal, closeModal } = useModal();

  const queryPageComp = ref('');
  const modelType = ref('');
  const selectedKeys = ref([]);
  const isLoading = ref(false);
  const renderData = ref();
  const total = ref(0);
  const removeAndEditorStatus = ['1', '4']; // 可删除的状态
  const voidStatus = ['5']; // ke

  // 数据查询
  const searchHandle = async (queryForm: any) => {
    const { data } = await getBasicDataContent(queryForm);
    console.log(data);
    renderData.value = data.list;
    total.value = data.total;
  };

  const reloadData = () => {
    const status = queryPageComp.value?.getStatus();

    searchHandle(status);
  };

  // 添加
  const addList = () => {
    modelType.value = 'add';
    openModal();
  };
  // 编辑
  const editorId = ref('');
  const editList = async (record: any) => {
    modelType.value = 'editor';
    editorId.value = record.id;
    openModal();
  };

  const removeHandle = async (idList: string[]) => {
    try {
      await deleteList(idList);
    } catch (e) {
      return;
    }
    Message.info('成功');
    reloadData();
  };
  const removeSingle = async (record: any) => {
    try {
      await modalPromise.warning({
        title: '提示',
        content: '您确定要删除当前所选的数据',
        hideCancel: false,
      });
    } catch (e) {
      return;
    }
    removeHandle([record.id]);
  };

  const removeBatch = async () => {
    const arr = renderData.value.filter((v) => {
      return selectedKeys.value.includes(v.id);
    });
    const status = arr.every(
      (v) =>
        v.documentPreparationStatus === '1' ||
        v.documentPreparationStatus === '4'
    );
    if (!status) {
      Message.warning('只有编制中或已驳回的数据可以删除！');
      return;
    }
    try {
      await modalPromise.info({
        title: '提示',
        content: '您确定要删除当前所选的数据？',
        hideCancel: false,
      });
    } catch {
      return;
    }
    const { data: res } = removeHandle(selectedKeys.value);
    if (res) Message.warning('删除成功');
    reloadData();
  };

  const voidHandle = async (idList: string[]) => {
    try {
      await listVoid(idList);
    } catch (e) {
      return;
    }

    Message.info('成功');

    reloadData();
  };

  const voidSingle = async (record: any) => {
    const targetData = voidStatus.includes(record.documentPreparationStatus);
    if (!targetData) {
      Message.warning('只有已发布的数据可以作废！');
      return;
    }
    try {
      await warning({
        title: '提示',
        content: '您确定要作废当前所选的数据',
        hideCancel: false,
      });
    } catch (e) {
      console.log('点击了取消');
      return;
    }

    voidHandle([record.id]);
  };

  const voidBatch = async () => {
    if (selectedKeys.value.length <= 0) {
      return;
    }
    const targetData = renderData.value.filter((item: any) => {
      return selectedKeys.value.includes(item.id);
    });
    const statusData = targetData.every((item) => {
      return voidStatus.includes(item.documentPreparationStatus);
    });
    if (!statusData) {
      Message.warning('只有已发布的的数据可以作废！');
      return;
    }
    const ids = targetData.map((item: any) => item.id);
    voidHandle(ids);
  };
</script>

<style scoped lang="less"></style>
